import {
	getEnterpriseList
} from '../../../api/app.js'
import {
	modifyPreciseExtension,
	preciseCustomInfo,
	getSalesPhone
} from "../../api/activity"
import {
	toast,
	confirm
} from "../../../utils/util"
import user from "../../../utils/user"
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		modify: {
			companyName: '',
			userName: '',
			companyCode: '',
			phone: '',
			id: '',
			area: ''
		},
		enterprise: {
			name: '',
			userName: '',
			code: '',
			phone: '',
			area: '',
			companyId: ''
		},
		type: 'alone',
		tipList: [],
		isAlonePopup: false,
		qrId: ''
	},
	onLoad(options) {
		const eventChannel = this.getOpenerEventChannel()
		eventChannel.on('acceptDataFromOpenerPage', (((data, type) => {
			console.log(data, data.type, 'data')
			this.setData({
				['enterprise.name']: data.data.name,
				['enterprise.phone']: data.data.phone,
				['enterprise.userName']: data.data.userName,
				['enterprise.code']: data.data.code,
				['enterprise.area']: data.data.area,
				['enterprise.companyId']: data.data.companyId,
				['modify.id']: data.data.id,
				type: data.type,
				qrId: data.data.qrId
			})
		})))
	},
	onShareAppMessage() {
		return {
			title: '「' + user.userName + '」' + ' 邀请您使用子弹发薪',
			imageUrl: '/packageActivity/assets/img/poster/invitation.png',
			path: '/packageActivity/pages/annually-spread/annually-spread?scene=' + encodeURIComponent('QRID=' + this.data.qrId),
		}
	},
	onNameInput(event) {
		console.log(event, 'name')
		const {
			value
		} = event.detail
		this.setData({
			['modify.userName']: value
		})
	},
	onPhoneInput(event) {
		const {
			value
		} = event.detail
		this.setData({
			['modify.phone']: value
		})
		console.log(event, 'Phone')
	},
	onEnterpriseInput(event) {
		console.log(event, 'Enterprise')
		const {
			value
		} = event.detail
		getEnterpriseList({
			name: value
		}).then(res => {
			console.log(res, 'res')
			this.setData({
				tipList: res
			})
		})
	},
	onTapEnter(event) {
		console.log(event, 'event')
		const {
			name,
			code,
			area,
			companyid
		} = event.currentTarget.dataset
		this.setData({
			['modify.companyName']: name,
			['modify.companyCode']: code,
			['modify.area']: area,
			tipList: [],
			isSendOutBtn: true,
			['enterprise.companyId']: companyid,
			['enterprise.name']: name,
			['enterprise.companyCode']: code,
			['enterprise.area']: area,
		})
		console.log(this.data.isSendOutBtn, 'isSendOutBtn')
	},
	cancel() {
		if (!this.data.modify.id) {
			toast('该企业已被提交,不可修改')
			return
		}
		this.setData({
			isAlonePopup: !this.data.isAlonePopup
		})
	},
	// 确定修改
	onPreciseExtension() {
		console.log(this.data.modify, 'this.data.modify')
		console.log(this.data.enterprise, 'this.data.enterprise')
		confirm('确认修改？').then(res => {
			this.setData({
				['modify.companyName']: this.data.modify.companyName || this.data.enterprise.name,
				['modify.phone']: this.data.modify.phone || this.data.enterprise.phone,
				['modify.userName']: this.data.modify.userName || this.data.enterprise.userName,
				['modify.companyCode']: this.data.modify.companyCode || this.data.enterprise.code,
				['modify.area']: this.data.modify.area || this.data.enterprise.area,
			})
			preciseCustomInfo({
				contactName: this.data.modify.userName,
				contactPhone: this.data.modify.phone,
				customName: this.data.modify.companyName,
				creditCode: this.data.modify.companyCode,
				id: this.data.modify.id,
				area: this.data.modify.area,
			}).then(res => {
				toast('修改成功')
				this.cancel()
				this.setData({
					['enterprise.name']: this.data.modify.companyName || this.data.enterprise.name,
					['enterprise.phone']: this.data.modify.phone || this.data.enterprise.phone,
					['enterprise.userName']: this.data.modify.userName || this.data.enterprise.userName,
					['enterprise.code']: this.data.modify.companyCode || this.data.enterprise.code,
				})
			})
		})
	},
	onMakePhoneCall() {
		wx.makePhoneCall({
			phoneNumber: this.data.enterprise.phone
		})

	},
	onDownloadPhone() {
		wx.getSetting({
			success(res) {
				wx.authorize({
					scope: 'scope.writePhotosAlbum',
					success: () => {
						wx.saveImageToPhotosAlbum({
							filePath: '/packageActivity/assets/img/maintain1.png',
						})
						wx.saveImageToPhotosAlbum({
							filePath: '/packageActivity/assets/img/maintain2.png',
						})
					}
				})
			}
		})
	},
	handleClick() {
		getSalesPhone({
			companyId: this.data.enterprise.companyId
		}).then(res => {
			console.log(111, res);
			wx.makePhoneCall({
				phoneNumber: res.phone
			})
		})
	}
})